package modules.news.models
{
	import flash.utils.getQualifiedClassName;
	import devdoo.fx.UseDB;
	import devdoo.fx.UseUtil;
	import devdoo.fx.UseString;
	import devdoo.fx.interfaces.ITableModel;
	
	public class UseNews implements ITableModel
	{
		private var _classNameModel:String = 'useNews';
		private var _table:String = 'news';
		private var _alias:String = 'USE_NEWS';
		private var _data:Object = new Object();
		private var _fields:Object = new Object();
		private var _idField:String = 'id';
		private var _idFieldAlias:String = 'USE_NEWS_id';
		private var _useTableAlias:Boolean;
		private var _isSave:Boolean = false;
		
		public function UseNews(){
			this._fields.USE_NEWS_breadcrumb_category_id = "breadcrumb_category_id";
			this._fields.USE_NEWS_breadcrumb_department_id = "breadcrumb_department_id";
			this._fields.USE_NEWS_breadcrumb_subcategory_id = "breadcrumb_subcategory_id";
			this._fields.USE_NEWS_changed_in = "changed_in";
			this._fields.USE_NEWS_created_in = "created_in";
			this._fields.USE_NEWS_favorite_total = "favorite_total";
			this._fields.USE_NEWS_finish_in = "finish_in";
			this._fields.USE_NEWS_id = "id";
			this._fields.USE_NEWS_is_active = "is_active";
			this._fields.USE_NEWS_last_changed_id = "last_changed_id";
			this._fields.USE_NEWS_newsletter = "newsletter";
			this._fields.USE_NEWS_origin = "origin";
			this._fields.USE_NEWS_origin_url = "origin_url";
			this._fields.USE_NEWS_rating_average = "rating_average";
			this._fields.USE_NEWS_rating_total = "rating_total";
			this._fields.USE_NEWS_start_in = "start_in";
			this._fields.USE_NEWS_use_contract_id = "use_contract_id";
			this._fields.USE_NEWS_user_register_id = "user_register_id";
			this._fields.USE_NEWS_view_total = "view_total";
		}
		
		public function get tableAlias():Object{
			var table:Object = new Object();
			table[this._alias] = this._table;
			return table;
		}
		
		public function get classNameModel():String{
			return this._classNameModel;
		}
		
		public function get table():String{
			return this._table;
		}
		
		public function get idField():String{
			return this._idField;
		}
		
		public function get idFieldAlias():String{
			return this._idFieldAlias;
		}
		
		public function get alias():String{ return this._alias; }
		
		public function fields(fieldsList:Array, useTableAlias:Boolean=false):Object{
			var data:Object = new Object();
			var fields:Object = this._fields;
			var errorFields:String = '';
			var error:Boolean = true;
			var item:String;
			var field:String;
			var i:String;
			var j:String;
			
			this._useTableAlias = useTableAlias;
			if(typeof(fieldsList[0]) == 'string' && fieldsList[0] == '*' ){
				if(useTableAlias){
					for ( field in this._fields){
						data[field] = this._fields[field];
					}
				}
				else{
					for ( field in this._fields){
						data[(this._fields[field])] = this._fields[field];
					}
				}
				for (item in this._setVar){
					data[item] = this._setVar[item];
				}
				for (item in this._expr){
					data[item] = this._expr[item];
				}
				return data;
			}
			for (j in fieldsList){
				if(useTableAlias){
					for (i in this._fields){
						if( fields[i] == fieldsList[j] ){
							data[this._alias+'_'+fields[i]] = fieldsList[j];
							error = false;
							continue;
						}
					}
				}
				else{
					for (i in fields){
						if(typeof(fieldsList[j])== 'object'){
							for (var k:String in fieldsList[j]) {
								data[k] = fieldsList[j][k];
								error = false;
								continue;
							}
						}
						else if( fields[i] == fieldsList[j] ){
							data[(fields[i])] = fieldsList[j];
							error = false;
							continue;
						}
					}
				}
				if(error) errorFields += fieldsList[j]+' :: ';
				error = true;
			}
			if(errorFields != ''){
				throw new Error(getQualifiedClassName(this) +'::Error( :: '+errorFields+')', 'Error :: Campos não encontrado no modelo da tabela '+this._table);
			}
			for (item in this._setVar){
				data[item] = this._setVar[item];
			}
			for (item in this._expr){
				data[item] = this._expr[item];
			}
			return data;
		}
		
		private var _expr:Object = new Object();
		public function expr(field:String, expr:String, alias:String = null, useTableAlias:Boolean=false):UseNews{
			var fields:Object = this._fields;
			var error:Boolean = true;
			for (var i:String in fields){
				if( fields[i] == field ) error = false;
			}
			if(error){
				throw new Error(getQualifiedClassName(this) +'::Error exp('+field+')', 'Error :: Campo utilizado em exp(?) não encontrado no modelo da tabela '+this._table);
				return null;
			}
			if(alias == null){
				if(useTableAlias)
					alias = this._alias + '_' + field;
				else
					alias = field;
			}
			else{
				if(useTableAlias)
					alias =  this._alias + '_' + alias;
			}
			this._expr[(alias)] = UseString.replace(expr, '?', this._alias + '.' + field) +' as ' + alias;
			return this;
		}
		
		private var _setVar:Object = new Object();
		public function setVar(field:String, value:String):UseNews{
			this._setVar[field] = value;
			return this;
		}
		public function get dataAlias():Object{
			var data:Object = {};
			data.USE_NEWS_breadcrumb_category_id = this.breadcrumb_category_id;
			data.USE_NEWS_breadcrumb_department_id = this.breadcrumb_department_id;
			data.USE_NEWS_breadcrumb_subcategory_id = this.breadcrumb_subcategory_id;
			data.USE_NEWS_changed_in = this.changed_in;
			data.USE_NEWS_created_in = this.created_in;
			data.USE_NEWS_favorite_total = this.favorite_total;
			data.USE_NEWS_finish_in = this.finish_in;
			data.USE_NEWS_id = this.id;
			data.USE_NEWS_is_active = this.is_active;
			data.USE_NEWS_last_changed_id = this.last_changed_id;
			data.USE_NEWS_newsletter = this.newsletter;
			data.USE_NEWS_origin = this.origin;
			data.USE_NEWS_origin_url = this.origin_url;
			data.USE_NEWS_rating_average = this.rating_average;
			data.USE_NEWS_rating_total = this.rating_total;
			data.USE_NEWS_start_in = this.start_in;
			data.USE_NEWS_use_contract_id = this.use_contract_id;
			data.USE_NEWS_user_register_id = this.user_register_id;
			data.USE_NEWS_view_total = this.view_total;
			return data;
		}
		public function get data():Object{
			var data:Object = {};
			data.breadcrumb_category_id = this.breadcrumb_category_id;
			data.breadcrumb_department_id = this.breadcrumb_department_id;
			data.breadcrumb_subcategory_id = this.breadcrumb_subcategory_id;
			data.changed_in = this.changed_in;
			data.created_in = this.created_in;
			data.favorite_total = this.favorite_total;
			data.finish_in = this.finish_in;
			data.id = this.id;
			data.is_active = this.is_active;
			data.last_changed_id = this.last_changed_id;
			data.newsletter = this.newsletter;
			data.origin = this.origin;
			data.origin_url = this.origin_url;
			data.rating_average = this.rating_average;
			data.rating_total = this.rating_total;
			data.start_in = this.start_in;
			data.use_contract_id = this.use_contract_id;
			data.user_register_id = this.user_register_id;
			data.view_total = this.view_total;
			return data;
		}
		public function set data(data:Object):void{
			var len:int = this._alias.length+1;
			var alias:String;
			var tempIndex:String;
			for (var index:* in data){
				alias = index.substring(0,len);
				if(alias == this._alias+'_'){
					tempIndex = index.replace(alias,'');
				}
				if(this.hasOwnProperty(tempIndex)){
					this[tempIndex] = data[index];
				}
			}
		}
		
		public function find(id:int, callBack:Function):void{
			var useDB:UseDB = new UseDB('SELECT');
			useDB.setFetchMode(UseDB.FETCH_OBJ)
				.from(tableAlias)
				.fields(['*'])
				.where(this._alias +'.'+ this.idField + ' = ?', String(id))
				.fetch('All')
				.connect(callBack);
		}
		
		public function remove(id:int, callBack:Function):void{
			var useDB:UseDB = new UseDB('DELETE');
			useDB.setFetchMode(UseDB.FETCH_OBJ)
			useDB.table(table)
				.where(this._alias +'.'+ this.idField + ' = ?', String(id))
				.connect(callBack);
		}
		
		public function update(callBack:Function):void{
			var useDB:UseDB = new UseDB('UPDATE');
			useDB.table(table)
				.data(this._data)
				.where(this.idField + ' = ?', this.id)
				.connect(callBack);
		}
		
		public function insert(callBack:Function):void{
			var useDB:UseDB = new UseDB('INSERT');
			useDB.table(table)
				.data(this._data)
				.connect(callBack);
		}
		
		public function fetch(callBack:Function, type:String = 'All'):void{
			var useDB:UseDB = new UseDB('SELECT');
			useDB.from(tableAlias)
				.fields(['*'])
				.fetch(type)
				.connect(callBack);
		}
		
		private var _breadcrumb_category_id:*;
		public function set breadcrumb_category_id(breadcrumb_category_id:*):void{
			this._breadcrumb_category_id = UseUtil.checkSetData('breadcrumb_category_id', breadcrumb_category_id);
			this._data['breadcrumb_category_id'] = breadcrumb_category_id;
			this._isSave = true;
		}
		public function get breadcrumb_category_id():*{ return this._breadcrumb_category_id; }
		
		private var _breadcrumb_department_id:*;
		public function set breadcrumb_department_id(breadcrumb_department_id:*):void{
			this._breadcrumb_department_id = UseUtil.checkSetData('breadcrumb_department_id', breadcrumb_department_id);
			this._data['breadcrumb_department_id'] = breadcrumb_department_id;
			this._isSave = true;
		}
		public function get breadcrumb_department_id():*{ return this._breadcrumb_department_id; }
		
		private var _breadcrumb_subcategory_id:*;
		public function set breadcrumb_subcategory_id(breadcrumb_subcategory_id:*):void{
			this._breadcrumb_subcategory_id = UseUtil.checkSetData('breadcrumb_subcategory_id', breadcrumb_subcategory_id);
			this._data['breadcrumb_subcategory_id'] = breadcrumb_subcategory_id;
			this._isSave = true;
		}
		public function get breadcrumb_subcategory_id():*{ return this._breadcrumb_subcategory_id; }
		
		private var _changed_in:*;
		public function set changed_in(changed_in:*):void{
			this._changed_in = UseUtil.checkSetData('changed_in', changed_in);
			this._data['changed_in'] = changed_in;
			this._isSave = true;
		}
		public function get changed_in():*{ return this._changed_in; }
		
		private var _created_in:*;
		public function set created_in(created_in:*):void{
			this._created_in = UseUtil.checkSetData('created_in', created_in);
			this._data['created_in'] = created_in;
			this._isSave = true;
		}
		public function get created_in():*{ return this._created_in; }
		
		private var _favorite_total:*;
		public function set favorite_total(favorite_total:*):void{
			this._favorite_total = UseUtil.checkSetData('favorite_total', favorite_total);
			this._data['favorite_total'] = favorite_total;
			this._isSave = true;
		}
		public function get favorite_total():*{ return this._favorite_total; }
		
		private var _finish_in:*;
		public function set finish_in(finish_in:*):void{
			this._finish_in = UseUtil.checkSetData('finish_in', finish_in);
			this._data['finish_in'] = finish_in;
			this._isSave = true;
		}
		public function get finish_in():*{ return this._finish_in; }
		
		private var _id:*;
		public function set id(id:*):void{
			this._id = UseUtil.checkSetData('id', id);
			this._data['id'] = id;
			this._isSave = true;
		}
		public function get id():*{ return this._id; }
		
		private var _is_active:*;
		public function set is_active(is_active:*):void{
			this._is_active = UseUtil.checkSetData('is_active', is_active);
			this._data['is_active'] = is_active;
			this._isSave = true;
		}
		public function get is_active():*{ return this._is_active; }
		
		private var _last_changed_id:*;
		public function set last_changed_id(last_changed_id:*):void{
			this._last_changed_id = UseUtil.checkSetData('last_changed_id', last_changed_id);
			this._data['last_changed_id'] = last_changed_id;
			this._isSave = true;
		}
		public function get last_changed_id():*{ return this._last_changed_id; }
		
		private var _newsletter:*;
		public function set newsletter(newsletter:*):void{
			this._newsletter = UseUtil.checkSetData('newsletter', newsletter);
			this._data['newsletter'] = newsletter;
			this._isSave = true;
		}
		public function get newsletter():*{ return this._newsletter; }
		
		private var _origin:*;
		public function set origin(origin:*):void{
			this._origin = UseUtil.checkSetData('origin', origin);
			this._data['origin'] = origin;
			this._isSave = true;
		}
		public function get origin():*{ return this._origin; }
		
		private var _origin_url:*;
		public function set origin_url(origin_url:*):void{
			this._origin_url = UseUtil.checkSetData('origin_url', origin_url);
			this._data['origin_url'] = origin_url;
			this._isSave = true;
		}
		public function get origin_url():*{ return this._origin_url; }
		
		private var _rating_average:*;
		public function set rating_average(rating_average:*):void{
			this._rating_average = UseUtil.checkSetData('rating_average', rating_average);
			this._data['rating_average'] = rating_average;
			this._isSave = true;
		}
		public function get rating_average():*{ return this._rating_average; }
		
		private var _rating_total:*;
		public function set rating_total(rating_total:*):void{
			this._rating_total = UseUtil.checkSetData('rating_total', rating_total);
			this._data['rating_total'] = rating_total;
			this._isSave = true;
		}
		public function get rating_total():*{ return this._rating_total; }
		
		private var _start_in:*;
		public function set start_in(start_in:*):void{
			this._start_in = UseUtil.checkSetData('start_in', start_in);
			this._data['start_in'] = start_in;
			this._isSave = true;
		}
		public function get start_in():*{ return this._start_in; }
		
		private var _use_contract_id:*;
		public function set use_contract_id(use_contract_id:*):void{
			this._use_contract_id = UseUtil.checkSetData('use_contract_id', use_contract_id);
			this._data['use_contract_id'] = use_contract_id;
			this._isSave = true;
		}
		public function get use_contract_id():*{ return this._use_contract_id; }
		
		private var _user_register_id:*;
		public function set user_register_id(user_register_id:*):void{
			this._user_register_id = UseUtil.checkSetData('user_register_id', user_register_id);
			this._data['user_register_id'] = user_register_id;
			this._isSave = true;
		}
		public function get user_register_id():*{ return this._user_register_id; }
		
		private var _view_total:*;
		public function set view_total(view_total:*):void{
			this._view_total = UseUtil.checkSetData('view_total', view_total);
			this._data['view_total'] = view_total;
			this._isSave = true;
		}
		public function get view_total():*{ return this._view_total; }
		
		public function get isSave():Boolean{ return this._isSave; }
		public function get useTableAlias():Boolean{ return this._useTableAlias; }
	}
}

